package com.ysu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysu.demo.dataobject.College;
import com.ysu.demo.dataobject.Project;
import com.ysu.demo.dto.ProjectDTO;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.enums.ProjectState;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.project.CollegeListForm;
import com.ysu.demo.from.project.ProjectApplyForm;
import com.ysu.demo.from.project.ProjectListPagesFrom;
import com.ysu.demo.mapper.ProjectMapper;
import com.ysu.demo.service.ICollegeService;
import com.ysu.demo.service.IExpendService;
import com.ysu.demo.service.IIncomeService;
import com.ysu.demo.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    @Autowired
    IExpendService expendService;

    @Autowired
    IIncomeService incomeService;

    @Autowired
    ICollegeService collegeService;

    @Override
    public IPage<Project> allList(ProjectListPagesFrom from) {
        Integer pages = (from.getPages() == null)?1:from.getPages();

        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();

        queryWrapper.isNull("delete_time");

        if(from.getState() != null){
            queryWrapper.eq("state",from.getState());
        }

        IPage<Project> res = this.page(new Page<Project>().setSize(20).setCurrent(pages),queryWrapper);

        return res;
    }

    @Override
    public ProjectDTO one(Integer id) {
        ProjectDTO dto = new ProjectDTO();

        Project project = this.getById(id);

        if(project == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        BeanUtils.copyProperties(project,dto);

//        List<Expend> expends = expendService.list(new QueryWrapper<Expend>()
//                .eq("project_id",dto.getId()));
//
//        List<Income> incomes = incomeService.list(new QueryWrapper<Income>()
//                .eq("project_id",dto.getId()));

        dto.setExpends(expendService.findDTO(id));
        dto.setIncomes(incomeService.findDTO(id));
        dto.setCollege(collegeService.getById(project.getCollegeId()));
        return dto;
    }

    @Override
    public Boolean changeState(ProjectState oldState,ProjectState newState,Integer id,String url) {
        Project project = this.getById(id);

        if(project == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        if(!project.getState().equals(oldState.getCode())){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        project.setState(newState.getCode());
        switch (project.getState()){
            case 1:
                project.setApproveUrl(url);
                break;
            case 2:
                project.setCloseUrl(url);
                break;
            default:
                throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        return this.updateById(project);
    }

    //初版未校验学院ID是否合法
    @Override
    public Boolean projectApply(ProjectApplyForm form) {
        if(collegeService.getById(form.getCollegeId()) == null){
            throw new BaseException(ExceptionEnum.NOT_FIND);
        }

        Project project = new Project();

        BeanUtils.copyProperties(form,project);

        project.setState(ProjectState.WAIT_GOD.getCode());

        return this.save(project);
    }

    //初版写法有误
    @Override
    public List<Project> collegeList(CollegeListForm form) {

        return this.list(new QueryWrapper<Project>()
                .eq("college_id",form.getCollegeId()));
    }
}
